/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.service.impl;

import java.util.List;

import com.legendshop.dao.support.CriteriaQuery;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.oa.constants.EventOperationTypeEnum;
import com.legendshop.oa.constants.EventTypeEnum;
import com.legendshop.oa.constants.ProjectStatusEnum;
import com.legendshop.oa.dao.AdminUserDao;
import com.legendshop.oa.dao.EventDao;
import com.legendshop.oa.dao.ProjectPlanDao;
import com.legendshop.oa.dto.ProjectPlanQueryDto;
import com.legendshop.oa.model.ProjectPlan;
import com.legendshop.oa.service.ProjectPlanService;
import com.legendshop.util.AppUtils;
import com.legendshop.util.TimerUtil;

/**
 * The Class ProjectPlanServiceImpl. 服务实现类
 */
public class ProjectPlanServiceImpl implements ProjectPlanService {

	/**
	 *
	 * 引用的Dao接口
	 */
	private ProjectPlanDao projectPlanDao;
	
	/**
	 * 用户
	 */
	private AdminUserDao adminUserDao;
	
	private EventDao eventDao;

	public void setProjectPlanDao(ProjectPlanDao projectPlanDao) {
		this.projectPlanDao = projectPlanDao;
	}

	/**
	 * 根据商城获取列表
	 */
	public List<ProjectPlan> getProjectPlan(String userName) {
		return projectPlanDao.getProjectPlan(userName);
	}

	/**
	 * 根据Id获取
	 */
	public ProjectPlan getProjectPlan(Long id) {
		return projectPlanDao.getProjectPlan(id);
	}

	/**
	 * 删除
	 */
	public void deleteProjectPlan(ProjectPlan projectPlan) {
		projectPlanDao.deleteProjectPlan(projectPlan);
	}

	/**
	 * 保存
	 */
	public Long saveProjectPlan(ProjectPlan projectPlan) {
		if (!AppUtils.isBlank(projectPlan.getId())) {
			//更新项目计划
			ProjectPlan origin = projectPlanDao.getProjectPlan(projectPlan.getId());
			if(origin!= null){
				String data = "";
				boolean changed = false;
				if(projectPlan.getUserName()!=null && !projectPlan.getUserName().equals(origin.getUserName())) {
					data = data + "修改了'负责人'， 旧值为" +  origin.getUserName() + ",新值为" +projectPlan.getUserName() + ";";
					changed = true;
				}
				
				if(AppUtils.isNotBlank(projectPlan.getStatus()) && !projectPlan.getStatus().equals(origin.getStatus())) {//修改了状态
					data = data + " 修改了'状态'， 旧值为" + ProjectStatusEnum.fromCode(origin.getStatus())  + ",新值为" + ProjectStatusEnum.fromCode(projectPlan.getStatus());
					changed = true;
				}
				
				if(AppUtils.isNotBlank(projectPlan.getTitle()) && !projectPlan.getTitle().equals(origin.getTitle())) {//修改了剩余时间
					data = data + " 修改了'标题'" + ",新值为" + projectPlan.getTitle();
					changed = true;
				}
				
				if(AppUtils.isNotBlank(projectPlan.getStartDate()) && !projectPlan.getStartDate().equals(origin.getStartDate())) {//修改开始时间
					data = data + " 修改了'开始时间'" + ",新值为" + TimerUtil.dateToStrShort(projectPlan.getStartDate());
					changed = true;
				}
				
				if(AppUtils.isNotBlank(projectPlan.getEndDate()) && !projectPlan.getEndDate().equals(origin.getEndDate())) {//修改了剩余时间
					data = data + " 修改了'结束时间'" + ",新值为" + TimerUtil.dateToStrShort(projectPlan.getEndDate());
					changed = true;
				}
				
				if(changed) {
					eventDao.saveEvent(projectPlan.getUserDetail(), origin.getId(), data, EventTypeEnum.PLAN, EventOperationTypeEnum.EDIT);
				}
				
				origin.setColor(projectPlan.getColor());
				origin.setEndDate(projectPlan.getEndDate());
				origin.setStartDate(projectPlan.getStartDate());
				origin.setWork(projectPlan.getWork());
				origin.setUserName(projectPlan.getUserName());
				origin.setStatus(projectPlan.getStatus());
				origin.setTitle(projectPlan.getTitle());
				origin.setModuleId(projectPlan.getModuleId());
				String userRealName = adminUserDao.getAdminUserByName(projectPlan.getUserName());
				origin.setUserRealName(userRealName);
				updateProjectPlan(origin);
				
			}
		
			return projectPlan.getId();
		}else{
			String userRealName = adminUserDao.getAdminUserByName(projectPlan.getUserName());
			projectPlan.setUserRealName(userRealName);
			projectPlan.setCreateBy(projectPlan.getUserDetail().getUsername());
			return projectPlanDao.saveProjectPlan(projectPlan);
		}

	}

	/**
	 * 更新
	 */
	public void updateProjectPlan(ProjectPlan projectPlan) {
		projectPlanDao.updateProjectPlan(projectPlan);
	}

	/**
	 * 查询列表
	 */
	public PageSupport getProjectPlan(CriteriaQuery cq) {
		return projectPlanDao.getProjectPlan(cq);
	}

	@Override
	public PageSupport<ProjectPlan> getProjectPlanByProject(ProjectPlanQueryDto projectPlanQueryDto) {
		return projectPlanDao.getProjectPlanByProject(projectPlanQueryDto);
	}

	public void setAdminUserDao(AdminUserDao adminUserDao) {
		this.adminUserDao = adminUserDao;
	}

	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}
}
